package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiMsg;
import com.lc.afbx.conn.PostApiRegist;
import com.lc.afbx.util.Validator;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/5/31.
 */
public class RegistActivity extends BaseActivity implements View.OnClickListener {


    private String getCode;//网络请求获取的验证码
    private TextView activity_registe_getcode_tv;
    private EditText activity_registe_phone_et;
    private EditText activity_registe_code_et;
    private EditText activity_registe_password_et;
    private EditText activity_registe_referrerName_et;
    private EditText activity_registe_referrerCode_et;
    private Button activity_registe_bt;

    private CountDownTimer countTime = new CountDownTimer(61000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            activity_registe_getcode_tv.setText(millisUntilFinished / 1000
                    + "s重新获取");
//                            time.setTextColor(getResources().getColor(R.color.white));
//                            time.setBackgroundColor(getResources().getColor(R.color.title_theme_color));
        }

        @Override
        public void onFinish() {
            activity_registe_getcode_tv.setText("重新获取");
            activity_registe_getcode_tv.setEnabled(true);
//                            time.setTextColor(getResources().getColor(R.color.bingdingtel_reget_code_text_color));
//                            time.setBackgroundColor(getResources().getColor(R.color.bingdingtel_reget_code_bg_color));

        }
    };

    private GetApiMsg getApiMsg = new GetApiMsg(new AsyCallBack<GetApiMsg.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiMsg.Info info) throws Exception {

            UtilToast.show(context, toast);

            getCode = info.getcode;

            countTime.start();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

            countTime.cancel();//取消计时
            activity_registe_getcode_tv.setText("获取验证码");
            activity_registe_getcode_tv.setEnabled(true);
        }
    });
    private PostApiRegist postApiRegist = new PostApiRegist(new AsyCallBack<PostApiRegist.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiRegist.Info info) throws Exception {

            UtilToast.show(context, "注册成功");

            startActivity(new Intent(RegistActivity.this, LoginActivity.class));

            finish();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registe);

        activity_registe_getcode_tv = ((TextView) findViewById(R.id.activity_registe_getcode_tv));
        activity_registe_phone_et = ((EditText) findViewById(R.id.activity_registe_phone_et));
        activity_registe_code_et = ((EditText) findViewById(R.id.activity_registe_code_et));
        activity_registe_password_et = ((EditText) findViewById(R.id.activity_registe_password_et));
        activity_registe_referrerName_et = ((EditText) findViewById(R.id.activity_registe_referrerName_et));
        activity_registe_referrerCode_et = ((EditText) findViewById(R.id.activity_registe_referrerCode_et));
        activity_registe_bt = ((Button) findViewById(R.id.activity_registe_bt));


        activity_registe_getcode_tv.setOnClickListener(this);
        activity_registe_bt.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.activity_registe_bt:

                if (activity_registe_phone_et.getText().toString().isEmpty()) {
                    UtilToast.show(this, "手机号不准为空");
                    return;
                }

                if (activity_registe_code_et.getText().toString().isEmpty()) {
                    UtilToast.show(context, "验证码不能为空");
                    return;
                }

                if (activity_registe_password_et.getText().toString().isEmpty()) {
                    UtilToast.show(this, "密码不准为空");
                    return;
                }

                if (!(activity_registe_code_et.getText().toString().equals(getCode))) {

                    if (getCode != null) {
//                        UtilLog.e("===========", getCode + "    " + v_code.getText().toString().trim());
                        UtilToast.show(this, "验证码错误");
                    } else {
                        UtilToast.show(this, "请点击获取验证码");
                    }

                    return;

                }

                if (!Validator.isMobile(activity_registe_phone_et.getText().toString())) {
                    UtilToast.show(context, "手机号格式错误");
                    return;
                }

                if (!activity_registe_phone_et.getText().toString().equals(getApiMsg.username)){
                    UtilToast.show(context, "请重新获取验证码");
                    return;
                }

                if (activity_registe_password_et.getText().toString().length() < 6) {
                    UtilToast.show(this, "密码最少6位");
                    return;
                }
                postApiRegist.username = activity_registe_phone_et.getText().toString().trim();
                postApiRegist.password = activity_registe_password_et.getText().toString().trim();
                postApiRegist.refcode = activity_registe_referrerCode_et.getText().toString().trim();
                postApiRegist.referrer = activity_registe_referrerName_et.getText().toString().trim();

                postApiRegist.execute(this);

                return;

            case R.id.activity_registe_getcode_tv:

                if (activity_registe_phone_et.getText().toString().isEmpty()) {
                    UtilToast.show(context, "请输入手机号");
                    return;
                }

//                UtilLog.e("====================", "" + UtilMatches.checkMobile(phoneNum.getText().toString()));
                if (!Validator.isMobile(activity_registe_phone_et.getText().toString())) {
                    UtilToast.show(context, "手机号格式错误");
                    return;
                }


                activity_registe_getcode_tv.setEnabled(false);
                //获取验证码接口
                getApiMsg.username = activity_registe_phone_et.getText().toString();
                getApiMsg.type = "reg";

                getApiMsg.execute(context);


                return;

        }


    }


}
